<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--{include file='comm/header.tpl'}-->
    <!--{include file='comm/comm_editor.tpl'}-->
    <!--{include file="comm/icheck.tpl"}-->
    <title>商品录入-生产信息</title>
</head>
<style>
.img-left{
	float:left;
	margin-left:12px;
}

.img-delete{
	width:160px;
	height:48px;
	line-height:48px;
	text-align:center;
}

.img-thumbnail{
	width:160px;
	height:160px;
}

#specSel{
	margin-top:6px;
	width:912px;
}

.form-rewrite{
	width:420px;
}

#fitSel,#intervalTime{
	margin-top:18px;
	width:650px;
}

#costTa table td{
	line-height:33px;
}

.grayBk{
	background:#CCCCCC;
}
</style>
<body>

<!--{include file='comm/naviBanner.tpl'}-->

<div class="ebsig_container">

    <!--{include file='comm/menu.tpl'}-->
    <div class="comm_content" style="margin-bottom:148px;">
    	<ul role="tablist" class="nav nav-tabs">
			<li class="group_box active">
				<a href="javascript:void(0);">生产信息</a>
			</li>
		</ul>
		
    	<div class="form-horizontal">
    		<!--用于判断是商品添加还是商品编辑-->
			<div class="form-group" style="width:800px;padding:10px;margin-bottom:0px;">
				<label class="col-sm-3 control-label" for="cityID" style="width:18%;"><span class="red">*</span> 商品所属城市：</label>
				<div class="col-sm-9" style="width:82%;">
					<select name="cityID" id="cityID" class="form-control w150" disabled>
						<option value="0">请选择</option>
						<!--{foreach from=$editInfo->city item=c}-->
							<option value="<!--{$c.id}-->"><!--{$c.name}--></option>
						<!--{/foreach}-->
					</select>
				</div>
			</div>
			
			<div class="form-group" style="width:800px;padding:10px;margin-bottom:0px;">
				<label class="col-sm-3 control-label" for="categoryID" style="width:18%;"><span class="red">*</span> 商品类目：</label>
				<div class="col-sm-9" style="width:82%;">
					<select name="categoryID" id="categoryID" class="form-control w150" disabled>
						<option value="0">请选择</option>
						<!--{foreach from=$editInfo->category item=c}-->
							<option value="<!--{$c.bigCategoryID}-->"><!--{$c.name}--></option>
						<!--{/foreach}-->
					</select>
				</div>
			</div>
			
			<div class="form-group" style="width:800px;padding:10px;margin-bottom:0px;">
				<label class="col-sm-3 control-label" for="goods-name" style="width:18%;"><span class="red">*</span> 商品名称：</label>
				<div class="col-sm-9" style="padding-left:0px;width:82%;">
					<div class="form-group">
						<label class="col-sm-3 control-label" for="goodsName" style="width:9%;">中文</label>
						<div class="col-sm-9" style="padding:0px;width:91%;">
							<input type="text" name="goodsName" id="goodsName" class="form-control w300" maxlength="15" disabled>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-3 control-label" for="frenchName" style="width:9%;">法文</label>
						<div class="col-sm-9" style="padding:0px;width:91%;">
							<input type="text" name="frenchName" id="frenchName" class="form-control w300" maxlength="30" disabled>
						</div>
					</div>
				</div>
			</div>
			
			<div class="form-group" id="goodsSpec" style="padding:10px;margin-bottom:0px;overflow-x:scroll;overflow-y:scroll;height:380px;">
				<label class="col-sm-3 control-label" for="spec" style="width:11.5%;padding-bottom:15px;"><span class="red">*</span> 商品规格：</label>
				<div class="col-sm-9" id="costTa" style="width:1480px;">
					<table class="table table-bordered">
						<thead>
							<tr>
								<!--{if $cost_permission || $detail_permission}-->
									<th width="12%" style="text-align:center;">操作</th>
								<!--{/if}-->
								<th width="6%" class="grayBk" style="text-align:center;">规格</th>
								<th width="6%" class="grayBk" style="text-align:center;">SKU编号</th>
								<th width="6%" class="grayBk" style="text-align:center;">售价</th>
								<!--{if $detail_permission}-->
									<th width="15%" style="text-align:center;">QS生产单品名</th>
									<th width="17%" style="text-align:center;">配料</th>
									<th width="8%" style="text-align:center;">净含量</th>
								<!--{/if}-->
								<!--{if $cost_permission}-->
									<th width="10%" class="cost hide" style="text-align:center;">不含税成本</th>
									<th width="10%" class="cost hide" style="text-align:center;">含税成本</th>
									<th width="10%" class="cost hide" style="text-align:center;">销售结算价</th>
								<!--{/if}-->
							</tr>
						</thead>
						<tbody>
							<!--{if isset($editInfo->saleGoods) && $editInfo->saleGoods != ''}-->
								<!--{foreach from=$editInfo->saleGoods item=sale}-->
									<tr>
										<!--{if $cost_permission || $detail_permission}-->
											<td style="text-align:center;">
												<!--{if $cost_permission}-->
													<a href="javascript:void(0);" onclick="goodsAdd.editCost();">编辑成本</a>
													&nbsp;
												<!--{/if}-->
												<!--{if $detail_permission}-->
													<a href="javascript:void(0);" onclick="goodsAdd.editDetail(<!--{$sale.postID}-->, '<!--{$sale.specName}-->');">编辑详情</a>
												<!--{/if}-->
											</td>
										<!--{/if}-->
										<td style="text-align:center;" class="grayBk"><!--{$sale.specName}--></td>
										<td class="grayBk"><!--{$sale.product_code}--></td>
										<td class="grayBk"><!--{$sale.salePrice}--></td>
										<!--{if $detail_permission}-->
											<td>
												<input id="qs_<!--{$sale.postID}-->" class="form-control" type="text" maxlength="200" <!--{if isset($sale.qsName)}--> value="<!--{$sale.qsName|default:''}-->" <!--{/if}--> style="width:100%;">
											</td>
											<td>
												<textarea id="ingre_<!--{$sale.postID}-->" class="form-control" style="height:33px;width:100%;"><!--{if isset($sale.recipe)}--> <!--{$sale.recipe|default:''}--> <!--{/if}--></textarea>
											</td>
											<td>
												<input id="net_<!--{$sale.postID}-->" class="form-control" maxlength="80" type="text" <!--{if isset($sale.weight)}--> value="<!--{$sale.weight|default:''}-->" <!--{/if}--> style="width:100%;">
											</td>
										<!--{/if}-->
										
										<!--{if $cost_permission}-->
											<!--{if isset($sale.noTaxCost) && $sale.noTaxCost > 0}-->
												<td class="cost hide grayBk"><!--{$sale.noTaxCost}--></td>
											<!--{else}-->
												<td class="cost hide" id="noInCost_<!--{$sale.postID}-->_td">
													<input id="noInCost_<!--{$sale.postID}-->" class="form-control" type="text" style="width:60px;display:inline-block;">
													<input class="btn btn-primary" type="button" onclick="goodsAdd.noInCost(<!--{$sale.postID}-->,'<!--{$sale.specName}-->');" value="保存">
												</td>
											<!--{/if}-->
											
											<!--{if isset($sale.taxCost) && $sale.taxCost > 0}-->
												<td class="cost hide grayBk"><!--{$sale.taxCost}--></td>
											<!--{else}-->
												<td class="cost hide" id="inCost_<!--{$sale.postID}-->_td">
													<input id="inCost_<!--{$sale.postID}-->" class="form-control" type="text" style="width:60px;display:inline-block;">
													<input class="btn btn-primary" type="button" onclick="goodsAdd.inCost(<!--{$sale.postID}-->,'<!--{$sale.specName}-->');" value="保存">
												</td>
											<!--{/if}-->
											
											<!--{if isset($sale.checkPrice) && $sale.checkPrice > 0}-->
												<td class="cost hide grayBk"><!--{$sale.checkPrice}--></td>
											<!--{else}-->
												<td class="cost hide" id="saleCost_<!--{$sale.postID}-->_td">
													<input id="saleCost_<!--{$sale.postID}-->" class="form-control" type="text" style="width:60px;display:inline-block;">
													<input class="btn btn-primary" type="button" onclick="goodsAdd.saleCost(<!--{$sale.postID}-->,'<!--{$sale.specName}-->');" value="保存">
												</td>
											<!--{/if}-->
										<!--{/if}-->
									</tr>
								<!--{/foreach}-->
							<!--{/if}-->
						</tbody>
					</table>
					
					<!--{if $detail_permission}-->
						<form name="addForm" id="addForm" method="POST" style="display:none;">
						
							<div class="form-group" style="width:800px;padding:10px;margin-bottom:0px;">
								<label class="col-sm-3 control-label" for="pGroupID" style="width:18%;"><span class="red">*</span> 生产信息维度：</label>
								<div class="col-sm-9" style="width:82%;">
									<div>
										<select name="pGroupID" id="pGroupID" class="form-control w300" multiple style="display:inline-block;height:110px;">
											<!--{foreach from=$editInfo->product item=p}-->
												<option value="<!--{$p.pGroupID}-->"><!--{$p.pGroupName}--></option>
											<!--{/foreach}-->
										</select>
										<span style="display:inline-block;vertical-align:middle;height:110px;">
											<button class="btn btn-primary" type="button" style="display:block;margin:15px 0 15px;" onclick="goodsAdd.batchProduct();">批量添加生产信息</button>
											<button class="btn btn-default" type="button" onclick="goodsAdd.openProduct();">生产信息维护</button>
										</span>
									</div>
									<div id="productSel">
										<table></table>
									</div>
								</div>
							</div>
							
							<div class="form-group" style="width:800px;padding:10px;margin-bottom:0px;margin-top:22px;">
								<label class="col-sm-3 control-label" for="nGroupID" style="width:18%;"><span class="red">*</span> 营养成分：</label>
								<div class="col-sm-9" style="width:82%;">
									<div>
										<select name="nGroupID" id="nGroupID" class="form-control w300" multiple style="display:inline-block;height:110px;">
											<!--{foreach from=$editInfo->nutrition item=n}-->
												<option value="<!--{$n.nGroupID}-->"><!--{$n.nGroupName}--></option>
											<!--{/foreach}-->
										</select>
										<span style="display:inline-block;vertical-align:middle;height:110px;">
											<button class="btn btn-primary" type="button" style="display:block;margin:15px 0 15px;" onclick="goodsAdd.batchNutri();">批量添加营养成分</button>
											<button class="btn btn-default" type="button" onclick="goodsAdd.openNutri();">营养成分信息维护</button>
										</span>
									</div>
									<div id="natriSel">
										<table></table>
									</div>
								</div>
							</div>
						</form>
						<div id="saveAll" style="width:800px;text-align:center;margin:38px 0 38px 0;display:none;">
					        <button type="button" onclick="goodsAdd.check();" class="btn btn-success">保存全部</button>
					    </div>
				    <!--{/if}-->
				</div>
			</div>
        </div>   
    </div>
</div>

<!--{include file='comm/footer.tpl'}-->
</body>
</html>
<script>
	
	$(function(){
		//编辑初始化
		<!--{if isset($editInfo) && $editInfo != ''}-->
			$('#cityID').val('<!--{$editInfo->cityID}-->');
			$('#categoryID').val('<!--{$editInfo->bigCategoryID}-->');
			$('#goodsName').val('<!--{$editInfo->goodsName}-->');
			$('#frenchName').val('<!--{$editInfo->frenchName}-->');
		<!--{/if }-->
		<!--{if !$cost_permission && !$detail_permission}-->
			$('#costTa').width('700px');
			$('#goodsSpec').css('overflow-x', 'hidden').css('overflow-y', 'hidden');
			$('.comm_content').css('margin-bottom','1px');
		<!--{elseif $cost_permission && !$detail_permission}-->	
			$('#costTa').width('904px');
			$('#goodsSpec').css('overflow-x', 'hidden').css('overflow-y', 'hidden');
			$('.comm_content').css('margin-bottom','1px');
		<!--{elseif !$cost_permission && $detail_permission}-->
			$('#costTa').width('1068px');
			$('#goodsSpec').css('overflow-x', 'hidden');
		<!--{/if }-->
	})
    
    //商品主信息添加操作对象
	var goodsAdd = {
		
		//三级商品网购号
		tPostID: 0,
		
		spec: '',
		
		dt: '',
		
		//编辑成本
		editCost: function(){
			$('.cost').removeClass('hide');
		},
		
		//编辑详情
		editDetail: function(postID, spec){
			if (!E.isInt(postID)) {
				E.alert('参数错误，请刷新页面后重新操作');
				return false;
			}
			
			this.tPostID = postID;
			this.spec = spec;
			$('#addForm').css('display', 'block');
			$('#saveAll').css('display', 'block');
			$('#goodsSpec').height('560px');
			
			//ajax查询已选‘生产信息’和‘营养成分’
			E.loadding.open();
			E.ajax_post({
				url : '/invokingAjax-backend/goods/goodsAdd.ajax?operFlg=9',
				data: {
					postID: postID,
				},
				call: 'goodsAdd.call'
			});
		},
		
		//生产信息维护
		openProduct: function(){
			window.open('/quanliadmin/app/goods/goodsProduce.pscript?operFlg=1');
		},
		
		//营养成分维护
		openNutri: function(){
			window.open('/quanliadmin/app/goods/goodsNutrition.pscript?operFlg=1');
		},
		
		//批量添加生产信息
		batchProduct: function(){
			var error_msg = '';
			var l = $('#pGroupID option:checked').length;
			if (l <= 0) {
				E.alert('请添加生产信息');
				return false;
			}
			
			var params = [];
			$('#pGroupID option:checked').each(function(k, v){
				var pid = $(v).attr('value');
				if ($('#product_' + pid).length > 0) {
					error_msg = '生产信息「'+$(v).text()+'」已添加';
					return false;
				}
				params.push(pid);
			})
			
			if (error_msg != '') {
				E.alert(error_msg);
				return false;
			}
			
			E.loadding.open();
			E.ajax_post({
				url : '/invokingAjax-backend/goods/goodsProduce.ajax?operFlg=7',
				data: {
					params: params
				},
				call: 'goodsAdd.productCall'
			});
		},
		
		//查询生产信息后回调
		productCall: function(o){
			E.loadding.close();
			if (o.code == 200) {
				$('#productSel table').append(o.data);
			} else {
				E.alert(o.message);
			}
		},
		
		//批量添加营养成分信息
		batchNutri: function(){
			var error_msg = '';
			var l = $('#nGroupID option:checked').length;
			if (l <= 0) {
				E.alert('请添加营养成分信息');
				return false;
			}
			
			var params = [];
			$('#nGroupID option:checked').each(function(k, v){
				var nid = $(v).attr('value');
				if ($('#nutri_' + nid).length > 0) {
					error_msg = '营养成分信息「'+$(v).text()+'」已添加';
					return false;
				}
				params.push(nid);
			})
			
			if (error_msg != '') {
				E.alert(error_msg);
				return false;
			}
			
			E.loadding.open();
			E.ajax_post({
				url : '/invokingAjax-backend/goods/goodsNutrition.ajax?operFlg=7',
				data: {
					params: params
				},
				call: 'goodsAdd.nutriCall'
			});
		},
		
		nutriCall: function(o){
			E.loadding.close();
			if (o.code == 200) {
				$('#natriSel table').append(o.data);
			} else {
				E.alert(o.message);
			}
		},
		
		pClear: function(flg){
			$('#product_' +flg).remove();
		},
		
		nClear: function(flg){
			$('#nutri_' +flg).remove();
		},
		
		//不含税成本
		noInCost: function(postID, spec){
			if (!E.isInt(postID)) {
				E.alert('参数错误，请刷新页面后重新操作');
				return false;
			}
			var cost = E.trim($('#noInCost_' + postID).val());
			if (!E.isNum(cost) || cost <= 0) {
				E.alert('请填写正确的不含税成本');
				return false;
			}
			this.cost = cost;
			this.costFlg = 1;
			this.costPostID = postID;
			E.confirm('您确定修改「' + spec + '」的不含税成本吗？修改后将不能再次编辑！', 'goodsAdd.confrim');
		},
		
		//含税成本
		inCost: function(postID, spec){
			if (!E.isInt(postID)) {
				E.alert('参数错误，请刷新页面后重新操作');
				return false;
			}
			
			var cost = E.trim($('#inCost_' + postID).val());
			if (!E.isNum(cost) || cost <= 0) {
				E.alert('请填写正确的含税成本');
				return false;
			}
			
			this.cost = cost;
			this.costFlg = 2
			this.costPostID = postID;
			E.confirm('您确定修改「' + spec + '」的含税成本吗？修改后将不能再次编辑！', 'goodsAdd.confrim');
		},
		
		//销售结算价
		saleCost: function(postID, spec){
			if (!E.isInt(postID)) {
				E.alert('参数错误，请刷新页面后重新操作');
				return false;
			}
			
			var cost = E.trim($('#saleCost_' + postID).val());
			if (!E.isNum(cost) || cost <= 0) {
				E.alert('请填写正确的销售结算价');
				return false;
			}
			
			this.cost = cost;
			this.costFlg = 3;
			this.costPostID = postID;
			E.confirm('您确定修改「' + spec + '」的销售结算价吗？修改后将不能再次编辑！', 'goodsAdd.confrim');
		},
		
		//确认修改
		confrim: function(){
			if (this.costFlg != 1 && this.costFlg != 2 && this.costFlg != 3) {
				E.alert('参数错误，请刷新页面后重新操作');
				return false;
			}
			if (!E.isInt(this.costPostID)) {
				E.alert('参数错误，请刷新页面后重新操作');
				return false;
			}
			
			E.loadding.open();
			E.ajax_post({
				url : '/invokingAjax-backend/goods/goodsAdd.ajax?operFlg=7',
				data: {
					postID: this.costPostID,
					flg: this.costFlg,
					cost: this.cost
				},
				call: 'goodsAdd.call'
			});
		},
		
		//回调
		call: function(o){
			E.loadding.close();
			if (o.code == 200) {
				if (o.data.type == 1) {	//保存税成本后回调
					E.alert(o.message, 2);
					switch(parseInt(o.data.flg)) {
						case 1:	//不含税成本
							$('#noInCost_' + o.data.postID + '_td').html(parseInt(o.data.cost).toFixed(2)).addClass('grayBk');
						break;
						case 2:	//含税成本
							$('#inCost_' + o.data.postID + '_td').html(parseInt(o.data.cost).toFixed(2)).addClass('grayBk');
						break;
						case 3:	//销售结算价
							$('#saleCost_' + o.data.postID + '_td').html(parseInt(o.data.cost).toFixed(2)).addClass('grayBk');
						break;
					}
				} else if (o.data.type == 2) {	//保存全部
					E.alert(o.message, 2);
				} else if (o.data.type == 3) {	//初始化显示
					if (o.data.productTr != '') {
						$('#productSel table').html(o.data.productTr);
						$.each(o.data.product_sel, function(k, v){
							$('#pGroupID_' + v.pGroupID).val(v.pid);
						});
					}
					if (o.data.nutriTr != '') {
						$('#natriSel table').html(o.data.nutriTr);
						$.each(o.data.nutrition_sel, function(k, v){
							$('#nGroupID_' + v.nGroupID).val(v.nid);
							$('#npct_' + v.nGroupID).val(v.npct);
						});
					}
				}
			} else {
				E.alert(o.message);
			}
		},
		
		//检查数据
		check: function(){
			if (!E.isInt(this.tPostID)) {
				E.alert('参数错误，请刷新页面后重新操作');
				return false;
			}

			var error_msg = '';
			var qsName = $('#qs_' + this.tPostID).val();
			if (qsName == '') {
				error_msg += '请填写「'+this.spec+'」商品的QS生产单品名<br/>';
			} else {
				qsName = E.trim($('#qs_' + this.tPostID).val())
			}

			var recipe = $('#ingre_' + this.tPostID).val();
			if (recipe == '') {
				error_msg += '请填写「'+this.spec+'」商品的配料信息<br/>';
			} else if (recipe.length > 500){
				error_msg += '配料信息不能大于500字符<br/>';
			} else {
				recipe = E.trim($('#ingre_' + this.tPostID).val());
			}

			var weight = $('#net_' + this.tPostID).val();
			if (weight == '') {
				error_msg += '请填写「'+this.spec+'」商品的净含量<br/>';
			} else {
				weight = E.trim($('#net_' + this.tPostID).val());
			}

			if (error_msg != '') {
				E.alert(error_msg);
				return false;
			}
			
			//生产信息维度
			var productLen = $('[name=selectPgroupID]').length;
			if (productLen <= 0) {
				E.alert('请添加生产信息维度');
				return false;
			}
			
			var msg = '';
			$('[name=selectPgroupID]').each(function(k, v){
				var pGroupID = $(this).val();
				var pid = $('#pGroupID_' + pGroupID).val();
				if (pid == 0 || pid == '') {
					msg = '请选择生产信息维度值';
					return false;
				}
			});
			if (msg != '') {
				E.alert(msg);
				return false;
			}
			
			//营养成分
			var nutriLen = $('[name=selectNgroupID]').length;
			if (nutriLen <= 0) {
				E.alert('请添加营养成分信息');
				return false;
			}
			
			$('[name=selectNgroupID]').each(function(k, v){
				var nGroupID = $(this).val();
				var nid = $('#nGroupID_' + nGroupID).val();
				var npct = E.trim($('#npct_' + nGroupID).val());
				if (nid == 0 || nid == '') {
					msg = '请选择营养成分信息值';
					return false;
				}
				if (npct == '') {
					msg = '请填写营养成分百分比';
					return false;
				}
			});
			if (msg != '') {
				E.alert(msg);
				return false;
			}
			
			this.dt = E.getFormValues('addForm');
			this.dt.postID = this.tPostID;
			this.dt.qsName = qsName;
			this.dt.recipe = recipe;
			this.dt.weight = weight;

			E.confirm('您确认保存「'+this.spec+'」商品的生产信息吗？', 'goodsAdd.save');
		},
		
		//保存生成信息
		save: function(){
			E.loadding.open();
			E.ajax_post({
				url : '/invokingAjax-backend/goods/goodsAdd.ajax?operFlg=8',
				data: {
					params: goodsAdd.dt,
				},
				call: 'goodsAdd.call'
			});
		},
		
		npct: function(nid){
			var npct = '';
			if (nid > 0) {
				npct = $('#nGroupID_' + nid + ' option:checked').attr('npct');
			}
			
			$('#npct_' + nid).val(npct);
		}
		
	}
</script>